package com.ocom.human.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.human.dao.IDaoEcardRegCode;
import com.ocom.human.service.EcardRegCodeService;
import com.ocom.common.entity.master.EntityEcardRegCode;
import com.ocom.mysql.aspect.DataSourceAspect;
import org.springframework.stereotype.Service;

import java.util.Date;


@Service
public class EcardRegCodeServiceImpl extends ServiceImpl<IDaoEcardRegCode, EntityEcardRegCode> implements EcardRegCodeService {


    @DataSourceAspect(type= DataSourceEnum.MAIN_DB,isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public EntityEcardRegCode getCardInfo(Long companyId, Long pId) {
        QueryWrapper<EntityEcardRegCode> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("com_id",companyId);
        queryWrapper.eq("p_id",pId);
        queryWrapper.ge("end_time",new Date());
        queryWrapper.last("limit 1");
        return this.getOne(queryWrapper);
    }

    @DataSourceAspect(type= DataSourceEnum.MAIN_DB,isMaster = MasterSlaveEnum.MASTER)
    @Override
    public EntityEcardRegCode addEcardRegCode(Long companyId, Long pId, String regCode, Date endTime) {
        EntityEcardRegCode entityEcardRegCode=new EntityEcardRegCode();
        entityEcardRegCode.setRegCode(regCode);
        entityEcardRegCode.setComId(companyId);
        entityEcardRegCode.setPId(pId);
        entityEcardRegCode.setEndTime(endTime);
        this.save(entityEcardRegCode);
        return entityEcardRegCode;
    }


}
